package com.example.service.impl;

import com.example.entity.Role;
import com.example.entity.Teacher;
import com.example.mapper.BookMapper;
import com.example.mapper.TeacherMapper;
import com.example.mapper.UserMapper;
import com.example.service.TeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @author： wzz
 * @date： 2021-10-18 20:20
 */
@Slf4j
@Service
public class TeacherServiceImpl implements TeacherService {

    @Resource
    private TeacherMapper teacherMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    BookMapper bookMapper;


    @Override
    public Integer ImportToMyBook(String classes, String book_name) {
        return teacherMapper.ImportToMyBook(classes,book_name);
    }

    @Override
    public Teacher GetTeacherByNum(String num) {
        Teacher teacher =teacherMapper.GetTeacherByNum(num);
        teacher.setRole(new Role("ROLE_teacher",1));

        return teacher;
    }

    @Override
    public List<Teacher> queryAll() {
        List<Teacher>  teacherList= teacherMapper.queryAll();
        for(Teacher teacher:teacherList){
            Role role = new Role();
            role.setROLE_id(userMapper.GetRoleId(teacher.getNum()));
            role.setROLE_name(userMapper.GerRoleName(role.getROLE_id()));
            teacher.setRole(role);
        }
        return teacherList;
    }


    @Override
    public Teacher queryOne(String jobNumber) {
        return teacherMapper.queryOne(jobNumber);
    }


    @Override
    public int delTeacher(String jobNumber) {
        return teacherMapper.delTeacher(jobNumber);
    }

    @Override
    public int addTeacher(Teacher teacher) {
        if (teacher.getPassword()==null) teacher.setPassword(teacher.getNum());
        userMapper.AddUser(teacher.getNum(),1);
        return teacherMapper.addTeacher(teacher);
    }

    @Override
    public int updataLevel(String jobNumber, String classes) {
        return teacherMapper.updataLevel(jobNumber, classes);
    }

    /**
     * 教师查询
     *
     * @param jobNumber
     * @param name
     * @return
     */
    @Override
    public List<Teacher> searchTeacher(String jobNumber, String name) {

        return teacherMapper.searchTeacher(jobNumber, name);
    }


    @Override
    public List<String> getBooks() {
        return teacherMapper.getBooks();
    }

    /**
     * 更新教师常规信息
     *
     * @param jobNumber
     * @param name
     * @param phone
     * @return
     */
    @Override
    public int updateTeacher(String jobNumber, String name, String phone) {
        return teacherMapper.updateTeacher(jobNumber, name, phone);
    }

    /**
     * 修改
     *
     * @param jobNumber
     * @param Books
     * @return
     */
    @Override
    public int addBooks(String jobNumber, String Books) {
        int result = 0;
        List<String> bookList = Arrays.asList(Books.split(","));

        log.info("classes：{}", bookList);
        for(String book:bookList) {
            result = teacherMapper.addBooks(jobNumber, book);
        }
        return result;
    }

    /**
     * 取消授课班级
     *
     * @return
     */
    @Override
    public int cancelTeacher(String Books) {
        int result = 0;
        String[] bookList = Books.split(",");
        for(String book:bookList){
            result = teacherMapper.cancelTeacher(book);
        }
        return result;
    }

    @Override
    public int cancelBooks(String num) {
        return teacherMapper.cancelBooks(num);
    }

    /**
     * 获取所有班级
     *
     * @return
     */
    @Override
    public List<String> getMyBooks(String num) {
        return teacherMapper.getMyBooks(num);
    }

    @Override

    public Boolean DeleteBookAbout(String book_name) {

        int success = 0;

        if (bookMapper.DelBooks(book_name)) success++;
        if (bookMapper.DelBookAboutTea(book_name)) success++;
        if (bookMapper.DelBookAboutStu(book_name)) success++;
        if (bookMapper.DelBookAboutMenu(book_name)) success++;
        return success == 4;
    }

}
